// pages/personal/myorder/myorder.js
import { orderAllList, cancelOrder, confirmReceipt, payOrder } from '../../../../config/config'
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: getApp().globalData.imgUrl,
    listCur: 0,
    status: '',
    page: 1,
    rows: 4,
    listData: [],
    typeData:[{
      id: 0,
      name: '全部',
      status: 'ALL'
    },{
      id: 1,
      name: '待付款',
      status: 'OBLIGATION'
    },{
      id: 7,
      name: '待成团',
      status: 'WAIT_GROUP'
    },{
      id: 2,
      name: '待收货',
      status: 'WAIT_SIGN'
    },{
      id: 3,
      name: '已完成',
      status: 'SUCCESS'
    }
    // ,{
    //   id: 4,
    //   name: '已取消',
    //   status: 'CANCEL'
    // }
  ],
    nomoreData: false,
    orderDlg: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let status = wx.getStorageSync('myorderStatus')
    console.log('页签', status)
    if(options) {
      // console.log('options.status', options.status)
      this.setData({
        status: options.status
      })
      wx.setStorageSync('myorderStatus', this.data.status)
      if (options.status == 'OBLIGATION') {
        this.setData({
          listCur: 1
        })
      } else if (options.status == 'WAIT_SIGN') {
        this.setData({
          listCur: 2
        })
      } else if (options.status == 'WAIT_GROUP') {
        this.setData({
          listCur: 7
        })
      }
    } else if(status) {
      this.setData({
        status: status
      })
    } else {
      this.setData({
        status: 'ALL'
      })
    }
  },
  // 获取订单列表
  getList() {
    let that = this
    // console.log('that.data.status', that.data.status)
    let paramsRaw = {
      status: that.data.status
    }
    let params = {
      page: that.data.page,
      rows: that.data.rows,
      paramsRaw: JSON.stringify(paramsRaw)
    }
    let listParams = JSON.stringify(params)
    app.reqFun.requestJson('POST',orderAllList,listParams,(res)=>{
      if(res.data.success === 1) {
        let records = res.data.data.records
      //  for(var i = 0, len = records.length; i < len; i++) {
      //     records[i].child[0].products[0].productAttr = JSON.parse(records[i].child[0].products[0].productAttr)
      //   }
        if ( that.data.rows*that.data.page >= res.data.data.total) {
          that.setData({
            nomoreData: true
          })
        }
        that.setData({
          listData: that.data.listData.concat(records)
        })
      }
    })
  },
  listSelect(e) {
    this.setData({
      listCur: e.currentTarget.dataset.id,
      status: e.currentTarget.dataset.status,
      page: 1,
      nomoreData: false,
      listData: []
     })
     wx.setStorageSync('myorderStatus', e.currentTarget.dataset.status)
     this.getList()
  },
  orderDetail(e) {
    let data = e.currentTarget.dataset.data
    let status = data.status || data.splitStatus
    let id =  data.parentId || data.id
    let orderId = data.orderId
    let option = {
      status: status,
      id: id,
      orderId: orderId
    }
    this.myorderStatus()
    wx.navigateTo({
      url: './orderDetail/orderDetail?option=' + JSON.stringify(option)
    })
  },
  // 关闭取消订单弹框
  onCloseorder() {
    this.setData({
      orderDlg: false
    })
  },
  orderCancelDlg(e) {
    let orderId = e.currentTarget.dataset.data
    this.setData({
      orderId: orderId,
      orderDlg: true
    })
  },
  // 取消订单
  orderCancel() {
    let orderId = this.data.orderId
    // let that = this
    let params = {
      orderId: orderId
    }
    app.reqFun.requestJson('POST',cancelOrder,params,(res)=>{
      if(res.data.success === 1) {
        this.setData({
          listCur: this.data.listCur,
          page: 1,
          nomoreData: false,
          listData: []
         })
         this.getList()
      }
    })
  },
  // 立即支付
  payOrder(e){
    let orderId = e.currentTarget.dataset.orderid
    let orderNo = e.currentTarget.dataset.orderno
    let orderType = e.currentTarget.dataset.ordertype
    let that=this
    let parms={
      orderNo:orderNo,
      paymentType:'SMALL_PROGRAM'
    }
    app.reqFun.requestJson('POST',payOrder,parms,(res)=>{
      if(res.data.success==1){
        var payData=res.data.data
        that.setData({
          payData:payData
        })
        that.watchPay(payData, orderType, orderId)
      }else{
        wx.showToast({
          title: '发起支付订单失败。',
          icon:'none'
        })
      }
    })
  },
  watchPay(payData, orderType, orderId){
    var that=this;
    wx.requestPayment({
      timeStamp: payData.timeStamp,
      nonceStr: payData.nonceStr,
      package: payData.packageValue,
      signType: payData.signType,
      paySign: payData.paySign,
      success (res) {
        if (orderType === 2 || orderType === 3) {
          app.couponNotifySubscribe(null, orderId)
        } else {
          app.moreOrderNotifySubscribe(null, orderId, orderType)
        }
        // wx.redirectTo({
        //   url: '../paySuccess/paySuccess?type=success',
        // })
      },
      fail (res) {
        wx.showModal({
          title: '支付失败',
          content: '支付失败,是否重新支付？',
          success (res) {
            if (res.confirm) { 
              that.watchPay(payData, orderType, orderId)
            } else if (res.cancel) {
              // wx.redirectTo({
              //   url: '../paySuccess/paySuccess?type=err',
              // })
             
            }
          }
        })
      }
    })
  },
  // 确认收货
  confirmReceipt(e) {
    let that = this
    wx.showModal({
      title: '提示',
      content: '是否确认收货？',
      success (res) {
        if (res.confirm) {
          let orderNo = e.currentTarget.dataset.data
          let params = {
            orderNo: orderNo
          }
          app.reqFun.requestJson('POST',confirmReceipt,params,(res)=>{
            if(res.data.success === 1) {
              that.setData({
                listCur: that.data.listCur,
                page: 1,
                nomoreData: false,
                listData: []
               })
               that.getList()
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  couponDetail(e) {
    let couponCode = e.currentTarget.dataset.data
    wx.navigateTo({
      url: '/packagMy/pages/coupon/couponDetail/couponDetail?couponCode=' + couponCode + '&isStoreDeposit=' + 1
    })
  },
  groupDetail(e) {
    let groupid = e.currentTarget.dataset.groupid
    wx.navigateTo({
      url: '/packageLive/pages/spellBuy/detail/detail?id=' + groupid,
    })
  },
  myorderStatus() {
    let that = this
    wx.setStorageSync('myorderStatus', that.data.status)
  },
  // logisticsDetail() {
  //   wx.navigateTo({
  //     url: '../logistics/logistics'
  //   })
  // },
  goreBack(e){
    wx.reLaunch({
      url: '/packagMy/pages/personal/index/index'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onLoad()
    this.setData({
      page: 1,
      rows: 4,
      nomoreData: false,
      listData: []
    })
    this.getList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.nomoreData) {
      this.setData({
        page: this.data.page + 1
      })
      this.getList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})